package com.blog.service.impl;

import com.blog.entity.Article;
import com.blog.entity.Comment;
import com.blog.entity.Filter;
import com.blog.mapper.ArticleMapper;
import com.blog.mapper.CommentMapper;
import com.blog.mapper.FileMapper;
import com.blog.service.DataViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DataViewServiceImpl implements DataViewService {

    private ArticleMapper articleMapper;

    private CommentMapper commentMapper;

    private FileMapper fileMapper;

    @Autowired
    public DataViewServiceImpl(ArticleMapper articleMapper, CommentMapper commentMapper, FileMapper fileMapper) {
        this.articleMapper = articleMapper;
        this.commentMapper = commentMapper;
        this.fileMapper = fileMapper;
    }

    @Override
    public List<Integer> getData() {
        List<Integer> data = new ArrayList<>();
        //查询出所有的文章
        List<Article> articles = articleMapper.query(null);
        Integer view=0;
        for (Article article : articles) {
            view=article.getArticleView()+view;
        }
        //文章置顶
        List<Article> articleTop = articles.stream().filter(x -> x.getTop() == 1).collect(Collectors.toList());
        List<Comment> comments = commentMapper.query(null);
        List<Filter> fileList = fileMapper.getFileList();
        data.add(articles.size());
        data.add(view);
        data.add(articleTop.size());
        data.add(comments.size());
        data.add(fileList.size());
        return data;
    }
}
